﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace PeaceBlog
{
    public static class MessageTypeHelper
    {
        public static string GetStringValue(this MessageType value)
        {
            Hashtable _stringValues = new Hashtable();
            string output = null;
            Type type = value.GetType();

            //Check first in our cached results...
            if (_stringValues.ContainsKey(value))
                output = (_stringValues[value] as StringValueAttribute).Value;
            else
            {
                //Look for our 'StringValueAttribute' 
                //in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                StringValueAttribute[] attrs =
                   fi.GetCustomAttributes(typeof(StringValueAttribute),
                                           false) as StringValueAttribute[];
                if (attrs.Length > 0)
                {
                    _stringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
                }
            }

            return output;
        }

        public static string GetStringValue(this LabelMessageType value)
        {
            Hashtable _stringValues = new Hashtable();
            string output = null;
            Type type = value.GetType();

            //Check first in our cached results...
            if (_stringValues.ContainsKey(value))
                output = (_stringValues[value] as StringValueAttribute).Value;
            else
            {
                //Look for our 'StringValueAttribute' 
                //in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                StringValueAttribute[] attrs =
                   fi.GetCustomAttributes(typeof(StringValueAttribute),
                                           false) as StringValueAttribute[];
                if (attrs.Length > 0)
                {
                    _stringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
                }
            }

            return output;
        }
    }

    public enum MessageType
    {
        [StringValue("error")]
        Error = 0,
        [StringValue("success")]
        Success = 1,
        [StringValue("warning")]
        Warning = 2,
        [StringValue("info")]
        Info = 3
    }

    public enum LabelMessageType
    {
        [StringValue("error")]
        Error = 0,
        [StringValue("success")]
        Success = 1,
        [StringValue("warning")]
        Warning = 2,
        [StringValue("info")]
        Info = 3
    }


    public class StringValueAttribute : System.Attribute
    {

        private string _value;

        public StringValueAttribute(string value)
        {
            _value = value;
        }

        public string Value
        {
            get { return _value; }
        }
    }
}